import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;

/**
 * LoginPanel
 * Panel with GUI to log on to they system
 *
 *
 *  Date           	Name            	Action
 *  --------------------------------------------------------------------------------------
 *	03/06/11		Bethany				Created class
 *
 *
 *
 * @author bethanysumner
 * @version $Revision: 1.0 $
 */
public class LoginPanel extends JPanel{
	/**
	 * The log file
	 */
	private Logs log;
	/**
	 * Field main.
	 */
	private MainWindow main;
	/**
	 * Field database.
	 */
	private Database database;
	
	/**
	 * Field counter.
	 */
	private int counter;
	/**
	 * Field txtUsername.
	 */
	private JTextField txtUsername;
	/**
	 * Field txtPassword.
	 */
	private JTextField txtPassword;
	/**
	 * Field btnLogin.
	 */
	private JButton btnLogin;
	/**
	 * Field userLevel.
	 */
	private int userLevel;
	
	
	/**
	 * LoginPanel constructor
	 * @param main A reference to the main window
	 * @param database A reference to the database
	 */
	public LoginPanel(MainWindow main, Database database) {
		
		this.main = main;
		this.database = database;
		this.log = new Logs();
		setLayout(null); // we are using absolute positioning
		
		// username field
		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setBounds(462, 52, 79, 16);
		add(lblUsername);
		txtUsername = new JTextField();
		txtUsername.setBounds(554, 46, 194, 28);
		add(txtUsername);
		
		// password field
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(462, 82, 118, 16);
		add(lblPassword);
		txtPassword = new JTextField();
		txtPassword.setBounds(554, 76, 194, 28);
		add(txtPassword);
		
		// login button
		ButtonListener buttonListener = new ButtonListener();
		btnLogin = new JButton("Login");
		btnLogin.setBounds(554, 127, 194, 29);
		btnLogin.addActionListener(buttonListener);
		add(btnLogin);

	}
	
	/**
	 * Logs user into the system with correct login credentials. Blocks login after 3 unsuccessful attempts.
	 * @param username The username
	 * @param password The password
	 * @return The usertype represented by 0-3 or -1 if login was unsuccessful * @throws ClassNotFoundException  * @throws SQLException  */
	public int login(String username, String password){
		
		int valid = -1;
		try {
			valid = database.checkValidUser(username, password);
			if (counter >=3){
				database.suspendUser(username);
				btnLogin.setEnabled(false);
				main.showError("Login is locked. You have made too many unsuccessful attempts. Please contact the system administrator.");
				log.logNonError(Level.SEVERE, "Three incorrect loggin attempts by username:" + username + ", and the password was:" + password);
				return -1;
			}else if (valid >= 0){
				if(database.isSuspended(username)){
					main.showError("The username: " + username + " is suspended. Please contact the system administrator.");
					log.logNonError(Level.SEVERE, "Suspended account attempted login. Username:" + username);
				}else{
					main.loginSuccessful(username, valid);
					return valid;
				}
			}else{
				counter++;
				main.showError("Login unsucessful. Incorrect credentials.");
				log.logNonError(Level.FINE, "Suspended account attempted login. Username:" + username + ", and password:" + password);
				return -1;
			}
		} catch (SQLException e) {
			main.showError("Error accessing database.");
			main.logError(e);
			e.printStackTrace();
		}
		
		return valid;
	
	}//end login method
	
	
	/**
	 * ButtonListener
	 * ActionListener to listen when button has been pressed
	 *
	 *
	 *  Date           	Name            	Action
	 *  --------------------------------------------------------------------------------------
	 *	03/06/11		Bethany				Created Class
	 *
	 *
	 *
	 * @author bethanysumner
	 * @version 1.0
	 */
	private class ButtonListener implements ActionListener{
		
		/**
		 * Performs tasks corresponding to the action
		 * @param event The action event
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
		 */
		public void actionPerformed(ActionEvent event){
			
			if(event.getSource().equals(btnLogin)){
				login(txtUsername.getText(), txtPassword.getText());
			}
			
		}
		
	}// end ButtonListener
}
